<?php
	class activity extends spController
	{
		function __construct()
		{
				// 必须加入启动父类构造函数的操作
				parent::__construct();
				 
				$this->a = $_GET['a'];
				$this->c = $_GET['c'];
		}

		function index()
		{
			$acid = intval($_GET['acid']);
			if($acid < 1) return false;
			$type = 'activity_' . $_GET['acid'];
			$m_activity = spClass('m_activity');
			$this->activity_result = $m_activity->find(array('acid'=>$acid));
			$m_weibo = spClass('m_weibo');
			$this->sport_result = $m_weibo->findAll(array('type'=>$type, 'cat'=>1));
			$this->life_result = $m_weibo->findAll(array('type'=>$type, 'cat'=>2));
			
			if($acid == 3) {
				$this->morning_result = $m_weibo->findAll(array('type'=>$type, 'cat'=>1));
				$this->middlenoon_result = $m_weibo->findAll(array('type'=>$type, 'cat'=>2));
				$this->afternoon_result = $m_weibo->findAll(array('type'=>$type, 'cat'=>3));
				$this->eveniong_result = $m_weibo->findAll(array('type'=>$type, 'cat'=>4));
			}
			//列出最热门的活动
			
			$this->top_youhui = spAccess('r',  'top_youhui');
			if(!$this->top_youhui) {
				$m_youhui = spClass("youhui");
				$condition = " status=0 AND isdel=0 AND enddate>=curdate()";
				$this->top_youhui = $m_youhui->findAll($condition, 'nums DESC', '*', '6');
				spAccess('w',  'top_youhui', $this->top_youhui, 3600);
			}

			$visitor_id = $_SESSION['visitor_id'];
			$visitor_nick = $_SESSION['visitor_nick'];

			$tpl = $this->activity_result['tpl'];
			$this->display('hd/' . $tpl .'.html');
		}

		function admin_list()
		{
			$m_activity = spClass('m_activity');
			$this->results = $m_activity->spPager($this->spArgs('page', 1), 10)->findAll($condition);
			$this->display('admin/acitvity_list.html');
		}

		function add_activity()
		{
			if(!empty($_POST)) {
				$m_activity = spClass('m_activity');
				$array['ac_name'] = $_POST['ac_name'];
				$array['tpl'] = $_POST['tpl'];
				$array['createdate'] = date('Y-m-d H:i:s', time());
				$m_activity->create($array);
				$this->success('提交成功', spUrl('activity', 'admin_list'));
			} else {
				$m_tempalte = spClass('m_template');
				$this->templates = $m_tempalte->select_template('hd');
				$this->display('admin/acitvity_add.html');
			}
		}

		function edit_activity()
		{
			$m_activity = spClass('m_activity');
			if(!empty($_POST)) {
				$array['ac_name'] = $_POST['ac_name'];
				$array['tpl'] = $_POST['tpl'];
				$acid = intval($_POST['acid']);
				$m_activity->update(array('acid'=>$acid), $array);
				$this->success('提交成功', spUrl('activity', 'admin_list'));
			} else {
				$acid  = intval($_GET['acid']);
				$this->results = $m_activity->find(array('acid'=>$acid));
				$m_tempalte = spClass('m_template');
				$this->templates = $m_tempalte->select_template('hd');
				$this->display('admin/acitvity_edit.html');
			}
		}

		/**
		 *  列出某个活动专题下的活动
		 */
		function list_activity_post()
		{
			$m_weibo = spClass("m_weibo");
			$this->acid = $_GET['acid'];
			$type = 'activity_' . $_GET['acid'];
			$condition = array('type'=>$type);
			$this->results = $m_weibo->spPager($this->spArgs('page', 1), 50)->findAll($condition);
			$this->pager = $m_weibo->spPager()->getPager();
			$this->display("admin/activity_list_post.html");
		}
		
		/**
		 *  添加某个活动专题下的活动
		 */
		function add_activity_post()
		{
			$m_activity = spClass('m_activity');
			if(!empty($_POST)) {
				$m_weibo = spClass("m_weibo");
				if(empty($_POST['url'])) $this->error('URL必须填写', spUrl('activity', 'admin_list'));
				$_POST['addtime'] = date('Y-m-d H:i:s', time());
				$m_attachment = spClass('attachment');
				$_POST['thumb'] = $m_attachment->upload('thumb');
				$m_weibo->create($_POST);
				$this->success('提交成功', spUrl('activity', 'list_activity_post', array('acid'=>$_POST['acid'])));
			} else {
				$this->m_fields = $m_fields = spClass('m_fields');
				$this->type = 'activity_' . $_GET['acid'];
				
				$arr_field = $m_fields->findAll(array('type'=>$this->type));
				foreach($arr_field as $k=>$field)
				{
					$field_result[$k]['output'] = $m_fields->output($field['field_id']);
					$field_result[$k]['field_name'] = $field['field_name'];
				}
				$this->field_result = $field_result;
				//var_dump($this->field_result);
				$this->acid  = intval($_GET['acid']);
				$this->results = $m_activity->find(array('acid'=>$acid));
				$this->display('admin/add_activity_post.html');
			}
		}

		/**
		 *  添加某个活动专题下的活动
		 */
		function edit_activity_post()
		{
			
			$m_weibo = spClass("m_weibo");
			if(!empty($_POST)) {
				$w_id = intval($_POST['w_id']);
				if($w_id < 1) return false;
				if(empty($_POST['url'])) $this->error('URL必须填写', spUrl('activity', 'edit_activity_post', array('w_id'=>$w_id, 'type'=>'activity_1')));
				$m_attachment = spClass('attachment');
				if($_FILES['thumb']['size'] > 0) {
					$_POST['thumb'] = $m_attachment->upload('thumb');
				}
				
				$m_weibo->update(array('w_id'=>$w_id), $_POST);
				
				$this->success('提交成功', spUrl('activity', 'list_activity_post', array('acid'=>$_POST['acid'])));
			} else {
				$m_activity = spClass('m_activity');
				$this->m_fields = $m_fields = spClass('m_fields');
				$this->type = $_GET['type'];
				$this->w_id = intval($_GET['w_id']);
				if($this->w_id < 1) return false;
				$this->result = $m_weibo->find(array('w_id '=>$this->w_id));
				
				$arr_field = $m_fields->findAll(array('type'=>$this->type));
				foreach($arr_field as $k=>$field)
				{
					$field_result[$k]['output'] = $m_fields->output($field['field_id'], $this->result['cat']);
					$field_result[$k]['field_name'] = $field['field_name'];
				}
				$this->field_result = $field_result;
			
				$this->acid = substr($this->type, strpos($this->type, '_')+1);
				
				$this->display('admin/edit_activity_post.html');
			}
		}
		
	}
?>